<?php
$hasError = isset($_SESSION['has_error']) ? $_SESSION['has_error'] : false;
$inputBag = isset($_SESSION['input_bag']) ? $_SESSION['input_bag'] : ['username' => ''];
$errorBag = isset($_SESSION['error_bag']) ? $_SESSION['error_bag'] : ['username' => '', 'password' => '', 'password_confirm' => ''];

// 销毁session中的错误数据包 （错误数据包是一次性的）
if (isset($_SESSION['has_error']) && isset($_SESSION['input_bag']) && isset($_SESSION['error_bag'])) {
   unset($_SESSION['has_error']);
   unset($_SESSION['input_bag']);
   unset($_SESSION['error_bag']);
}
?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>留言板-注册</title>
  <link rel="stylesheet" type="text/css" href="<?php echo assets('css/form.css')?>">
</head>
<body>
  <div id="wrapper">
    <form method="post" action="<?php echo url('register_action') ?>" enctype="multipart/form-data">
      <div class="row">
        <div class="input-box">
          <label>用户名</label>
          <input type="text" name="username" placeholder="请输入用户名" value="<?=$inputBag['username'] ?>">
        </div>
        <div class="error">
          <?php if ($hasError) echo $errorBag['username'] ?>
        </div>
      </div>
      <div class="row">
        <div class="input-box">
          <label>密&nbsp;&nbsp;&nbsp;&nbsp;码</label>
          <input type="password" name="password" placeholder="请输入密码">
        </div>
        <div class="error">
            <?php if ($hasError) echo $errorBag['password'] ?>
           </div>
      </div>
      <div class="row">
        <div class="input-box">
          <label>确认密码</label>
          <input type="password" name="password_confirm" placeholder="再次输入密码确认">
        </div>
        <div class="error">
            <?php if ($hasError) echo $errorBag['password_confirm'] ?>
           </div>
      </div>
      <div class="row">
           <div class="input-box">
             <label>头像</label>
             <input type="file" name="avatar">
           </div>
           <div class="error">
            <?php if ($hasError) echo $errorBag['avatar'] ?>
           </div>
      </div>
      <div class="row">
          <button class="btn-large">立即注册</button>
      </div>
      <div class="info">
        已有账号？现在<a href="<?php echo url('signin') ?>">登录</a>
      </div>
    </form>
  </div>
</body>
</html>